from django.db import models


# Create your models here.


# 友情链接表
class FriendLink(models.Model):
	name = models.CharField('网站名称', max_length=50)
	description = models.CharField('网站描述', max_length=100, blank=True)
	link = models.URLField('友链地址', help_text='请填写http或https开头的完整形式地址')
	logo = models.URLField('网站LOGO', help_text='请填写http或https开头的完整形式地址', blank=True)
	created_time = models.DateTimeField('创建时间', auto_now_add=True)
	is_active = models.BooleanField('是否有效', default=True)
	is_show = models.BooleanField('是否首页展示', default=False)

	class Meta:
		verbose_name = '友情链接'
		verbose_name_plural = verbose_name
		ordering = ['created_time']

	def __str__(self):
		return self.name

	def get_home_url(self):
		"""提取友链的主页"""
		u = re.findall(r'(http|https://.*?)/.*?', self.link)
		home_url = u[0] if u else self.link
		return home_url
	#是否有效方法
	def active_to_false(self):
		self.is_active=False
		self.save(update_fields=['is_active'])
	#是否显示方法
	def show_to_false(self):
		self.is_show = True
		self.save(update_fields=['is_show'])




